<template>
  <ContentWrap style="overflow: visible">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="公海名称" prop="poolName">
        <el-input v-model="queryParams.poolName"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="flex justify-between">
          <div>
            <el-button @click="handleQuery"
              ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
            >

            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['crm:clue-pool:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table ref="sortTable" :data="list" :stripe="true" :show-overflow-tooltip="true" border>
      <el-table-column label="公海名称" prop="poolName" width="100px" />
      <el-table-column label="使用范围" prop="type" width="100px">
        <template #default="scope">
          <div>
            <span v-if="scope.row.type == 0">全公司</span>
            <span v-if="scope.row.type == 1">指定部门</span>
            <span v-if="scope.row.type == 2">指定员工</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="指定部门" prop="deptName" />
      <el-table-column label="指定员工" prop="userName" />

      <el-table-column label="操作" align="center" width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', false, scope.row.id)"
            v-hasPermi="['crm:clue-pool:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:clue-pool:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <CluePoolForm ref="clueFormRef" @success="getList"></CluePoolForm>
</template>
<script setup lang="ts">
import * as CluePoolApi from '@/api/crm/cluePool'
import CluePoolForm from './CluePoolForm.vue'
const message = useMessage() // 消息弹窗
const total = ref(0)
const queryParams = reactive({
  poolName: '',
  pageNo: 1,
  pageSize: 10
})
const list = ref([])
const getList = async () => {
  const data = await CluePoolApi.getCluePoolPage(queryParams)
  list.value = data.list
  total.value = data.total
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
// 编辑线索表单
/** 添加/修改操作 */
const clueFormRef = ref()
const openForm = (type: string, isShow: boolean, id?: number) => {
  clueFormRef.value.open(type,isShow, id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CluePoolApi.deleteCluePool(id)
    message.success('删除成功')
    // 刷新列表
    await getList()
  } catch {}
}
onMounted(() => {
  getList()
})
</script>
